package rolesystem.middle.utilityclasses;

import java.io.Serializable;

import rolesystem.middle.managers.EnvironmentManager;

public class PositionSq implements Serializable
{
	/**
	 * The x coordinate.
	 */
	public int x;
	
	/**
	 * The y coordinate.
	 */
	public int y;
	
	/**
	 * Void constructor 
	 */
	public PositionSq()
	{
	}
	
	/**
	 * Constructor. Set the passed coordinates on the internal value.
	 * 
	 * @param pos_x the x coordinate
	 * @param pos_y the y coordinate
	 */
	public PositionSq(final int pos_x,final int pos_y)
	{
		this.x=pos_x;
		this.y=pos_y;
	}
	
	/**
	 * Constructor. Set the passed coordinates on the internal value.
	 * 
	 * @param pos the coordinates to set
	 */
	public PositionSq(final PositionSq pos)
	{
		this.x=pos.x;
		this.y=pos.y;
	}
	
	
	/**
	 * Returns true if the coordinates are both zero.
	 * 
	 * @return the boolean that contains the result of the check
	 */
	public boolean isZero()
	{
		if(this.x==0 && this.y==0)
			return true;
		else
			return false;
	}
	
	/**
	 * Set the passed coordinates on the internal value.
	 * 
	 * @param  pos_x  the x coordinate
	 * @param  pos_y  the y coordinate
	 */
	public void set(final int pos_x,final int pos_y)
	{
		this.x=pos_x;
		this.y=pos_y;
	}
	
	/**
	 * Set the coordinates on the internal value.
	 * 
	 * @param pos the coordinates to set
	 */
	public void set(final PositionSq pos)
	{
		this.x=pos.x;
		this.y=pos.y;
	}
	
	/**
	 * Return the x coordinate.
	 * 
	 * @param pos the x coordinate
	 */
	public int getX()
	{
		return this.x;
	}
	
	/**
	 * Return the y coordinate.
	 * 
	 * @return the y coordinate
	 */
	public int getY()
	{
		return this.y;
	}
	
	/**
	 * Add the passed coordinates to the internal value.
	 * 
	 * @param  incAmount_x  the x coordinate to add
	 * @param  incAmount_y  the y coordinate to add
	 */
	public void add(final int incAmount_x,final int incAmount_y)
	{
		this.x=this.x+incAmount_x;
		this.y=this.y+incAmount_y;
	}
	
	/**
	 * Add the passed coordinates to the internal value.
	 * 
	 * @param incAmount the coordinates to add
	 */
	public void add(final PositionSq incAmount)
	{
		this.x=this.x+incAmount.x;
		this.y=this.y+incAmount.y;
	}
	
	/**
	 * Subtract the passed coordinates from the internal value.
	 * 
	 * @param  diffAmount_x  the x coordinate to subtract
	 * @param  diffAmount_y  the y coordinate to subtract
	 */
	public void diff(final int diffAmount_x,final int diffAmount_y)
	{
		this.x=this.x-diffAmount_x;
		this.y=this.y-diffAmount_y;
	}
	
	/**
	 * Subtract the passed coordinates from the internal value.
	 * 
	 * @param diffAmount the coordinates to subtract
	 */
	public void diff(final PositionSq diffAmount)
	{
		this.x=this.x-diffAmount.x;
		this.y=this.y-diffAmount.y;
	}
	
	/**
	 * Change the sign of the internal coordinates.
	 */
	public void neg()
	{
		this.x=-this.x;
		this.y=-this.y;
	}
	
	/**
	 * Check if the internal coordinates have a valid value (they don't reference a Square outside the Environment dimension).
	 * 
	 * @return true if the coordinates don't have a valid value 
	 */
	public boolean checkBounds()
	{
		if( x >= EnvironmentManager.ENV_MAX_X || y >= EnvironmentManager.ENV_MAX_Y || x < 0 || y < 0)
			return true;
		else
			return false;
	}
	
	/**
	 * Check if the internal coordinates have a valid value (they don't reference a Square outside the Environment dimension) and
	 * corrects them.
	 */
	public void checkAndCorrectBounds()
	{
		if(x<0)
			x=0;
		
		if(y<0)
			y=0;
		
		if(x>=EnvironmentManager.ENV_MAX_X)
			x=EnvironmentManager.ENV_MAX_X-1;
		
		if(y>=EnvironmentManager.ENV_MAX_Y)
			y=EnvironmentManager.ENV_MAX_Y-1;
	}
	
	/**
	 * Check if the internal coordinates have the same value of the passed (external) ones.
	 * 
	 * @param positionCheck the external coordinates
	 * @return true if the two sets of coordinates have the same value. 
	 */
	public boolean equalTo(final PositionSq positionCheck)
	{
		if(positionCheck.x==x && positionCheck.y==y )
			return true;
		else
			return false;
	}

	/**
	 * Subtract the second passed coordinate from the first one.
	 * 
	 * @param position01 the "reference" coordinates.
	 * @param position02 the coordinates to subtract from the first ones.
	 * @return the result of the subtraction
	 */
	public static PositionSq diff(final PositionSq position01,final PositionSq position02)
	{
		return new PositionSq(position01.x-position02.x,position01.y-position02.y);
	}
	
	/**
	 * Add the first coordinate to the second one.
	 * 
	 * @param  position01  the first coordinate to add
	 * @param  position02  the second coordinate to add
	 * @return the result of the addition
	 */
	public static PositionSq add(final PositionSq position01,final PositionSq position02)
	{
		return new PositionSq(position01.x+position02.x,position01.y+position02.y);
	}
	
	/**
	 * Change the sign of the passed coordinates.
	 * 
	 * @param  position01 the coordinates
	 * @return the result of the operation
	 */
	public static PositionSq neg(final PositionSq position01)
	{
		return new PositionSq(-position01.x,-position01.y);
	}
	
	/**
	 * Check if the coordinates have a valid value (they don't reference a Square outside the Environment dimension).
	 * 
	 * @param pos the coordinates to check
	 * @return true if the coordinates don't have a valid value 
	 */
	public static boolean checkBounds(final PositionSq pos)
	{
		if( pos.x >= EnvironmentManager.ENV_MAX_X || pos.y >= EnvironmentManager.ENV_MAX_Y ||pos.x < 0 || pos.y < 0)
			return true;
		else
			return false;
	}
	
	/**
	 * Check if the coordinates have a valid value (they don't reference a Square outside the Environment dimension) and
	 * corrects them.
	 * 
	 * @param pos the coordinates to check and correct
	 * @return the corrected coordinates 
	 */
	public static PositionSq checkAndCorrectBounds(PositionSq pos)
	{
		if(pos.x<0)
			pos.x=0;
		
		if(pos.y<0)
			pos.y=0;
		
		if(pos.x>=EnvironmentManager.ENV_MAX_X)
			pos.x=EnvironmentManager.ENV_MAX_X-1;
		
		if(pos.y>=EnvironmentManager.ENV_MAX_Y)
			pos.y=EnvironmentManager.ENV_MAX_Y-1;
		
		return pos;
	}
	
	/**
	 * Check if the two passed coordinates have the same value.
	 * 
	 * @param position01 one of the external coordinates
	 * @param position02 the other external coordinate
	 * @return true if the two sets of coordinates have the same value. 
	 */
	public static boolean equalTo(final PositionSq position01, final PositionSq position02)
	{
		if(position01.x==position02.x && position01.y==position02.y )
			return true;
		else
			return false;
	}

}



